package com.meilai.project.controller.web.finance.supplier;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.dto.finance.supplier.SupplierDTO;
import com.meilai.project.dto.finance.supplier.SupplierListDTO;
import com.meilai.project.dto.finance.supplier.SupplierMaterialDTO;
import com.meilai.project.entity.finance.supplier.Supplier;
import com.meilai.project.service.finance.supplier.SupplierService;
import com.meilai.project.service.system.DictionaryService;
import com.meilai.project.vo.finance.payment.PaymentApplyVO;
import com.meilai.project.vo.finance.supplier.SupplierWebVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "web端供应商控制器")
@RestController
@RequestMapping("api/web/supplier")
public class WebSupplierController {

    @Autowired
    private SupplierService service;
    @Autowired
    private DictionaryService dictionaryService;

    @GetMapping("list-by-material-id")
    @ApiOperation(value = "根据材质id，查询供应商List")
    public ApiResult<List<Supplier>> getListByMaterialId(@ApiParam("materialId") Long materialId) {
        return ApiResult.ok(service.getListByMaterialId(materialId));
    }

    @GetMapping("list")
    @ApiOperation(value = "列表分页")
    public ApiResult<Page<SupplierWebVO>> list(@ApiParam("form") SupplierListDTO req) {
        Page<SupplierWebVO> voPage = service.selectPageList(req);
        return ApiResult.ok(voPage);
    }

    @GetMapping("no_page_list")
    @ApiOperation(value = "列表不分页")
    public ApiResult<List<SupplierWebVO>> noPageList(@ApiParam("form") SupplierListDTO req) {
        return ApiResult.ok(service.selectList(req));
    }

    @GetMapping("detail")
    @ApiOperation(value = "供应商详情")
    public ApiResult<PaymentApplyVO> detail(
            @ApiParam("id") @RequestParam(value = "id") Long id
    ) {
        return ApiResult.ok(service.getOneById(id));
    }

    @GetMapping("detail_material")
    @ApiOperation(value = "物料详情")
    public ApiResult<PaymentApplyVO> detailMaterial(
            @ApiParam("id") @RequestParam(value = "id") Long id
    ) {
        return ApiResult.ok(service.getMaterialById(id));
    }

    @GetMapping("dict_pay_cycle")
    @ApiOperation(value = "结算周期字典")
    public ApiResult<PaymentApplyVO> dictPayCycle() {
        return ApiResult.ok(dictionaryService.listByCode("gysxg-jszq"));
    }

    @GetMapping("dict_delivery_method")
    @ApiOperation(value = "送货方式字典")
    public ApiResult<PaymentApplyVO> dictDeliveryMethod() {
        return ApiResult.ok(dictionaryService.listByCode("gysxg-shfs"));
    }

    @GetMapping("dict_material_texture")
    @ApiOperation(value = "材质字典")
    public ApiResult<PaymentApplyVO> dictMaterialTexture() {
        return ApiResult.ok(dictionaryService.listByCode("gysxg-cz"));
    }

    @PostMapping("add")
    @ApiOperation("新增供应商")
    public ApiResult<Boolean> add(@ApiParam("json") @Valid @RequestBody SupplierDTO req) {
        try {
            if (service.add(req)) {
                return ApiResult.ok("新增成功");
            } else {
                return ApiResult.err("新增失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PostMapping("edit")
    @ApiOperation("编辑供应商")
    public ApiResult<Boolean> edit(@ApiParam("json") @Valid @RequestBody SupplierDTO req) {
        try {
            if (service.edit(req)) {
                return ApiResult.ok("编辑成功");
            } else {
                return ApiResult.err("编辑失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @DeleteMapping("delete")
    @ApiOperation("删除供应商")
    public ApiResult<Boolean> delete(@ApiParam("id") @RequestParam(value = "id") Long id) {
        try {
            if (service.delete(id)) {
                return ApiResult.ok("删除成功");
            } else {
                return ApiResult.err("删除失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @PostMapping("save_material")
    @ApiOperation("编辑物料")
    public ApiResult<Boolean> saveMaterial(@ApiParam("json") @Valid @RequestBody SupplierMaterialDTO req) {
        try {
            if (service.saveMaterial(req)) {
                return ApiResult.ok("编辑成功");
            } else {
                return ApiResult.err("编辑失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }
}
